/*
 * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_lpuart.h"
#include "peripherals.h"
#include "board.h"
#include "common_inc.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define ECHO_BUFFER_LENGTH 10
#define DEMO_LPUART          LPUART3

/*******************************************************************************
 * Prototypes
 ******************************************************************************/
uint32_t value;
float vol;
//uint8_t data[4]={1,2,3,4};
lpuart_handle_t g_lpuartHandle;
/*******************************************************************************
 * Code
 ******************************************************************************/
uint8_t g_tipString[] =
    "Lpuart interrupt example\r\nBoard receives 8 characters then sends them out\r\nNow please input:\r\n";

volatile bool rxBufferEmpty            = true;
volatile bool txBufferFull             = false;
volatile bool txOnGoing                = false;
volatile bool rxOnGoing                = false;
void LPUART_UserCallback(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData)
{
      userData = userData;

    if (kStatus_LPUART_TxIdle == status)
    {
        txBufferFull = false;
        txOnGoing    = false;
    }

    if (kStatus_LPUART_RxIdle == status)
    {
        rxBufferEmpty = false;
        rxOnGoing     = false;
    }
}

void UserCallBack(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData)
{
      userData = userData;

    if (kStatus_LPUART_TxIdle == status)
    {
        txBufferFull = false;
        txOnGoing    = false;
    }

    if (kStatus_LPUART_RxIdle == status)
    {
        rxBufferEmpty = false;
        rxOnGoing     = false;
    }
}

void UserCallBack2(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData)
{
      userData = userData;

    if (kStatus_LPUART_TxIdle == status)
    {
        txBufferFull = false;
        txOnGoing    = false;
    }

    if (kStatus_LPUART_RxIdle == status)
    {
        rxBufferEmpty = false;
        rxOnGoing     = false;
    }
}
/*!
 * @brief Main function
 */
int main(void)
{

//	lpuart_transfer_t xfer;
//    /* Init board hardware. */
//    BOARD_ConfigMPU();
//    BOARD_InitBootPins();
//	  
//    BOARD_InitBootClocks();
//   //BOARD_InitDebugConsole();
//	
//		/* Set the PWM Fault inputs to a low value */
//		//(IOMUXC_GPIO_SD_B0_00_FLEXPWM1_PWMA00,0x10B0);
////    XBARA_Init(XBARA1);
////    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm1Fault0);
////    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm1Fault1);
////    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm1234Fault2);
////    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm1234Fault3);
////	  XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm2Fault0);
////    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm2Fault1);
//		BOARD_InitBootPeripherals();

//    /* Just enable the trace clock, leave coresight initialization to IDE debugger */
////    SystemCoreClockUpdate();
////    CLOCK_EnableClock(kCLOCK_Trace);

//    //PRINTF("hello world.\r\n");
//	
//		//UserMain();
//		
//		
////		PWM_SetPwmLdok(PWM1, kPWM_Control_Module_0, true);
////    PWM_StartTimer(PWM1, kPWM_Control_Module_0);

//	 /* Send g_tipString out. */


//    while (1)
//    {
//			
//			
//			if ((!rxOnGoing) && rxBufferEmpty)
//        {
//            rxOnGoing = true;
//            LPUART_TransferReceiveNonBlocking(LPUART3, &LPUART3_handle, &LPUART3_rxTransfer, NULL);
//        }

//        /* If TX is idle and g_txBuffer is full, start to send data. */
//        if ((!txOnGoing) && txBufferFull)
//        {
//            txOnGoing = true;
//            LPUART_TransferSendNonBlocking(LPUART3, &LPUART3_handle, &LPUART3_txTransfer);
//        }

//        /* If g_txBuffer is empty and g_rxBuffer is full, copy g_rxBuffer to g_txBuffer. */
//        if ((!rxBufferEmpty) && (!txBufferFull))
//        {
//            memcpy(LPUART3_txBuffer, LPUART3_rxBuffer, 10);
//            rxBufferEmpty = true;
//            txBufferFull  = true;
//        }
//			//LPUART_TransferReceiveNonBlocking(LPUART3,&LPUART3_handle,&LPUART3_rxTransfer,NULL);
//			
//			
//	
////			  ADC_SetChannelConfig(ADC1, ADC1_CH0_CONTROL_GROUP, &ADC1_channels_config[3]);
////			 while(0U == ADC_GetChannelStatusFlags(ADC1, ADC1_CH0_CONTROL_GROUP))
////        {
////  
////				
////				}
////				value = ADC_GetChannelConversionValue(ADC1, ADC1_CH0_CONTROL_GROUP);
////				vol = value * 3.3f / 4096.0f;
//        //ch = GETCHAR();
//        //PUTCHAR(ch);
////			GPIO_PinWrite(GPIO8,4U,1); 
////			GPIO_PinWrite(GPIO1,9U,1); 
////			PWM_SetPwmLdok(PWM1, kPWM_Control_Module_0, false);
////      PWM_UpdatePwmDutycycle(PWM1,kPWM_Module_0,kPWM_PwmA,kPWM_EdgeAligned,50);
////      PWM_SetPwmLdok(PWM1, kPWM_Control_Module_0, true);
//    }
		lpuart_config_t config;
    lpuart_transfer_t xfer;
    lpuart_transfer_t sendXfer;
    lpuart_transfer_t receiveXfer;

    BOARD_ConfigMPU();
    BOARD_InitBootPins();
    BOARD_InitBootClocks();
	  BOARD_InitBootPeripherals();
		
		
		XBARA_Init(XBARA1);
    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm1Fault0);
    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm1Fault1);
    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm1234Fault2);
    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm1234Fault3);
	  XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm2Fault0);
    XBARA_SetSignalsConnection(XBARA1, kXBARA1_InputLogicHigh, kXBARA1_OutputFlexpwm2Fault1);
		
		TickInit();
		UserMain();
		
//		while(1){
//		    xfer.data     = g_tipString;
//    xfer.dataSize = sizeof(g_tipString) - 1;
//    txOnGoing     = true;
//    LPUART_TransferSendNonBlocking(LPUART3, &BOARD_LPUART3_handle, &xfer);

//    /* Wait send finished */
//    while (txOnGoing)
//    {
//    }
		
		

    /*
     * config.baudRate_Bps = 115200U;
     * config.parityMode = kLPUART_ParityDisabled;
     * config.stopBitCount = kLPUART_OneStopBit;
     * config.txFifoWatermark = 0;
     * config.rxFifoWatermark = 0;
     * config.enableTx = false;
     * config.enableRx = false;
     */
//    LPUART_GetDefaultConfig(&config);
//    config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
//    config.enableTx     = true;
//    config.enableRx     = true;

//    LPUART_Init(DEMO_LPUART, &config, DEMO_LPUART_CLK_FREQ);
//    LPUART_TransferCreateHandle(DEMO_LPUART, &g_lpuartHandle, LPUART_UserCallback, NULL);

    /* Send g_tipString out. */
//    xfer.data     = g_tipString;
//    xfer.dataSize = sizeof(g_tipString) - 1;
//    txOnGoing     = true;
//    LPUART_TransferSendNonBlocking(DEMO_LPUART, &LPUART3_handle, &xfer);

//    /* Wait send finished */
//    while (txOnGoing)
//    {
//    }

    /* Start to echo. */
//    LPUART3_txTransfer.data        = LPUART3_txBuffer;
//    LPUART3_txTransfer.dataSize    = LPUART3_TX_BUFFER_SIZE;
//    LPUART3_rxTransfer.data     = LPUART3_rxBuffer;
//    LPUART3_rxTransfer.dataSize = LPUART3_RX_BUFFER_SIZE;

//    while (1)
//    {
//        /* If RX is idle and g_rxBuffer is empty, start to read data to g_rxBuffer. */
//        if ((!rxOnGoing) && rxBufferEmpty)
//        {
//            rxOnGoing = true;
//            LPUART_TransferReceiveNonBlocking(DEMO_LPUART, &LPUART3_handle, &LPUART3_rxTransfer, NULL);
//        }

//        /* If TX is idle and g_txBuffer is full, start to send data. */
//        if ((!txOnGoing) && txBufferFull)
//        {
//            txOnGoing = true;
//            LPUART_TransferSendNonBlocking(DEMO_LPUART, &LPUART3_handle, &LPUART3_txTransfer);
//        }

//        /* If g_txBuffer is empty and g_rxBuffer is full, copy g_rxBuffer to g_txBuffer. */
//        if ((!rxBufferEmpty) && (!txBufferFull))
//        {
//            memcpy(LPUART3_txBuffer, LPUART3_rxBuffer, ECHO_BUFFER_LENGTH);
//            rxBufferEmpty = true;
//            txBufferFull  = true;
//        }
//    }
}
